C�digo Fonte
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||C�digo do Microcontrolador|||||||||||||||||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include <16f877A.h>
#device adc=10
#use delay(clock=11059200)
#fuses HS,NOWDT,PUT,NOLVP
#use rs232(baud=9600, xmit=PIN_C6,rcv=PIN_C7)
#include
#include
//--------------------------------------------------------------------------------------
char a[6]={000000}, string[4]={0000},opid,mode[2];
boolean aux=0;
long int x, pwm_r=0,pwm_c=0;
int PV=0,HIST=0,aux1, aux2,K=1,Ti=5,Td=0,Ki,Kp,Kd,tmp300=2, h=0,buffer=0;
long int MV_SUP,MV_INF,PWM;
boolean q=0;
signed long int erro=0,erro1=0,erro2=0;
float uk=0,uk1=0,T=0.3,SP=40.0,value;
//-------------------------------------------------
// Interrupcao Serial
//-------------------------------------------------
#INT_RDA
void recebe()
{
buffer=0;
while(buffer!=6)
{
a[buffer]=getc();
buffer++;
}
aux=1;
}
//-------------------------------------------------
void read_string()
{
string[0]=a[3];
string[1]=a[4];
string[2]=a[5];
string[3]='\0';
}
void PID()
{
Ki=(K*T)/Ti;
Kp=K-(Ki/2);
Kd=(K*Td)/T;
value = (float)read_adc();
value*=0.0048828;
value*=100;
erro=SP-value;
uk=uk1+(Kp+Ki+Kd)*erro-(Kp+Kd)*erro1+Kd*erro2;
PWM=(long int)uk;
if(PWM>1023)
PWM=1023;
else{
if(PWM<0)
PWM=0;
}
uk1=uk;
erro2=erro1;
erro1=erro;
set_pwm2_duty ( PWM );
}
//--------------------------------------------------------------------------------------
// INTERRUPCAO TIMER para PID
//--------------------------------------------------------------------------------------
#int_timer1
void timer1()
{//1
tmp300--;
if(tmp300==0)
{
tmp300=2;
PID();
if(input(PIN_D6)==1)
output_D(0);
else
output_D(255);
}
set_timer1(13633);
}//1
//-------------------------------------------------
// Main()
//-------------------------------------------------
main()
{
int *p;
//-----------------------------------------
p=&SP;
setup_timer_1 ( T1_INTERNAL | T1_DIV_BY_8 );
set_timer1(13633);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
//-----------------------------------------
setup_ADC_ports (ALL_ANALOG);
setup_adc( ADC_CLOCK_INTERNAL );
set_adc_channel( 0 );
//-----------------------------------------
setup_timer_2 (T2_DIV_BY_4, 248, 1);
setup_ccp1 (ccp_pwm);
set_pwm1_duty ( 0 );
setup_ccp2 (ccp_pwm);
set_pwm2_duty ( 0 );
//-----------------------------------------
while (true)
{
if(aux==1)
{
mode[0]=a[0];
mode[1]=a[1];
opid=a[2];
aux=0;
delay_ms(100);
}
//----------------------------------------
string[0]=a[3];
string[1]=a[4];
string[2]=a[5];
string[3]='\0';
x = atol(string);
x*=1024/100;
switch( opid )
{//6
delay_ms(100);
case 'R':
{
set_pwm1_duty (x);
pwm_r=x;
delay_ms(3);
printf("ACKR");
delay_ms(3);
a[0]='\0';
printf("%c",a[0]);
break;
}
case 'C':
{
pwm_c=x;
set_pwm2_duty (pwm_c);
delay_ms(3);
printf("ACKC");
delay_ms(3);
a[0]='\0';
printf("%c",a[0]);
break;
}
case 'L':
{
value = read_adc();
delay_ms(10);
value*=0.0048828;
value*=100;
printf("L");
PV=(int)value;
printf("%u", PV);
a[0]='\0';
printf("%c",a[0]);
strcpy(a,"00000");
break;
}
}//6
switch( mode[0] )
{// 0
case 'A':
{//1
disable_interrupts(int_timer1);
delay_ms(10);
value = read_adc();
delay_ms(10);
value*=0.0048828;
value*=100;
PV=(int)value;
a[0]='\0';
switch( mode[1] )
{//2
read_string();
case 'L':
{//5
SP=atoi(string);
aux1=SP-HIST;
aux2=SP+HIST;
break;
}//5
case 'R':
{//1.1
delay_ms(10);
if(PVaux2)
{//1.4
set_pwm1_duty( MV_INF );
}//1.4
}//1.3
break;
}//1.1
case 'C':
{//1.1
delay_ms(10);
if(PVaux2)
{//1.4
set_pwm2_duty( MV_SUP );
}//1.4
}//1.3
break;
}//1.1
case 'S':
{//6
MV_SUP=atol(string);
break;
}//6
case 'I':
{//6
MV_INF=atol(string);
break;
}//6
case 'H':
{//6
HIST=atoi(string);
aux1=SP-HIST;
aux2=SP+HIST;
break;
}//6
}//2
break;
}//1
case 'P':
{
switch( mode[1] )
{
case 'K':
{
K=atoi(string);
break;
}
case 'I':
{
TI=atoi(string);
break;
}
case 'D':
{
TD=atoi(string);
break;
}
case 'E':
{
enable_interrupts(int_timer1);
break;
}
break;
}
break;
}
}//0
//----------------------------------------
opid='\0';
delay_ms(150);
}
}
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||C�digo do Borland||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include
#pragma hdrstop
#include
#include
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CGAUGES"
#pragma link "QCCom32"
#pragma link "ComPort"
#pragma resource "*.dfm"
TForm1 *Form1;
bool popup = false,control_track_bar = false;
char opid[2]="00",modo[2]="--";
int valor[2],tempo;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Edit1->Text = 0;
Edit2->Text = 0;
Edit3->Text = 0;
Panel2->Width = 74;
Panel2->Height = 2;
Panel2->Top = 14;
Panel2->Visible = false;
Image2->Visible = false;
Image3->Visible = false;
Edit1->MaxLength= 3;
Edit2->MaxLength= 3;
StatusBar1->Panels->operator [](0)->Text = "Com Desligada";
StatusBar1->Panels->operator [](1)->Text = "Gr�fico Pausado";
ComPort1->EnumComDevicesFromRegistry(ComboBox1->Items);
Timer5->Enabled = false;
Button3->Caption = "Liga";
Configurar->Enabled = false;
Abrir_Porta->Enabled= false;
opid[0] = '\xff';
opid[1] = '\0';
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TrackBar1Change(TObject *Sender)
{
if(control_track_bar==true)
Edit1->Text = (TrackBar1->Position)*10;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TrackBar2Change(TObject *Sender)
{
if(control_track_bar==true)
Edit2->Text = (TrackBar2->Position)*10;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit3Change(TObject *Sender)
{
valor[1]=atoi(Edit3->Text.c_str());
// CGauge1->Progress = StrToInt(Edit3->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
String frame;
char a[5];
int tamanho;
if(ComPort1->Active==1)
frame = ComPort1->ReadString();
strcpy(a,frame.c_str());
tamanho = frame.Length();
if (tamanho > 0)
{
if(a[0]=='A')
{
switch(a[3])
{
case 'R': // ackR
{
Image2->Visible=true;
break;
}
case 'C': // ackC
{
Image3->Visible=true;
break;
}
}
}
if(a[0]=='L' && opid[1]=='L')
{
opid[0]='L';
a[0] = a[1];
a[1] = a[2];
a[2] = a[3];
a[3] = '\0';
if(a[0]=='1' || a[0]=='2' || a[0]=='3' || a[0]=='4' || a[0]=='5' || a[0]=='6' || a[0]=='7' || a[0]=='8' || a[0]=='9' || a[0]=='0')
{
if(a[1]=='1' || a[1]=='2' || a[1]=='3' || a[1]=='4' || a[1]=='5' || a[1]=='6' || a[1]=='7' || a[1]=='8' || a[1]=='9' || a[1]=='0')
{
Label7->Caption=a+String("�");
CGauge1->Progress=atoi(Label7->Caption.c_str());
}
}
}
Timer3->Enabled = true;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1Change(TObject *Sender)
{
int pwm1;
Timer1->Enabled = false;
opid[1]=opid[0];
opid[0]='R';
pwm1=atoi(Edit1->Text.c_str());
if(pwm1>100){ pwm1=100; Edit1->Text="100"; }
TrackBar1->Position=pwm1/10;
Timer3->Enabled = true;
Image2->Visible = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit2Change(TObject *Sender)
{
int pwm2;
Timer1->Enabled = false;
opid[1]=opid[0];
opid[0]='C';
pwm2=atoi(Edit2->Text.c_str());
if(pwm2>100){ pwm2=100; Edit2->Text="100"; }
TrackBar2->Position=pwm2/10;
Timer3->Enabled = true;
Image3->Visible=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer2Timer(TObject *Sender)
{
if(popup==false)
{
Label9->Color = clTeal;
Panel2->Visible = true;
if(Panel2->Width<200)
Panel2->Width+=6;
else
{
if(Panel2->Height<90)
Panel2->Height+=3;
else
{
Timer2->Enabled = false;
Label9->Color = clTeal;
popup = true;
}
}
}
else
{
if(Panel2->Height>2)
Panel2->Height-=3;
else
{
if(Panel2->Width>74)
Panel2->Width-=6;
else
{
Timer2->Enabled = false;
Panel2->Visible = false;
Label9->Color = clMoneyGreen;
popup = false;
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Label9Click(TObject *Sender)
{
Timer2->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Label9MouseEnter(TObject *Sender)
{
Label9->Color = clTeal;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Label9MouseLeave(TObject *Sender)
{
if(Panel2->Visible==false)
Label9->Color = clMoneyGreen;
else
Label9->Color = clTeal;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TrackBar1Enter(TObject *Sender)
{
control_track_bar = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TrackBar1Exit(TObject *Sender)
{
control_track_bar = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TrackBar2Enter(TObject *Sender)
{
control_track_bar = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TrackBar2Exit(TObject *Sender)
{
control_track_bar = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer3Timer(TObject *Sender)
{
char tring[3];
if(ComPort1->Active==1)
{
switch (opid[0])
{
case 'R':
{
StrCopy(tring,Edit1->Text.c_str());
if(Edit1->Text=="")
{
tring[0] = '0';
tring[1] = '\0';
}
break;
}
case 'C':
{
StrCopy(tring,Edit2->Text.c_str());
if(Edit2->Text=="")
{
tring[0] = '0';
tring[1] = '\0';
}
break;
}
case 'L':
{
tring[0] = '\0';
tring[1] = '\0';
break;
}
default:
{
opid[0] = '\0';
break;
}
}
if(opid!='\0')
{
ComPort1->WriteChar(modo[0]);
ComPort1->WriteChar(modo[1]);
ComPort1->WriteChar(opid[0]);
if(strcmp(tring,"100")==0)
ComPort1->WriteString(100);
else
{
ComPort1->WriteChar('0');
for(int aux=0;aux<2;aux++)
ComPort1->WriteChar(tring[aux]);
}
}
Timer3->Enabled = false;
Timer1->Enabled = true;
opid[1]=opid[0];
opid[0]='L';
}
}
//---------------------------------------------------------------------------
GR�FICO
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
tempo = 0;
Profundidade->Position = Grafico->Chart3DPercent;
Rotacao->Position = Grafico->View3DOptions->Rotation;
Perspectiva->Position = Grafico->View3DOptions->Perspective;
Elevacao->Position = Grafico->View3DOptions->Elevation;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::D3Click(TObject *Sender)
{
Grafico->View3DWalls = D3->Checked;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OrtogonalClick(TObject *Sender)
{
Grafico->View3DOptions->Orthogonal = Ortogonal->Checked;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ProfundidadeChange(TObject *Sender)
{
Grafico->Chart3DPercent = Profundidade->Position;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RotacaoChange(TObject *Sender)
{
Grafico->View3DOptions->Rotation = Rotacao->Position;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PerspectivaChange(TObject *Sender)
{
Grafico->View3DOptions->Perspective = Perspectiva->Position;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ElevacaoChange(TObject *Sender)
{
Grafico->View3DOptions->Elevation = Elevacao->Position;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ZoomoutClick(TObject *Sender)
{
Grafico->ZoomPercent(60);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ZoominClick(TObject *Sender)
{
Grafico->ZoomPercent(120);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LimparClick(TObject *Sender)
{
Series1->Clear();
Series2->Clear();
Grafico->BottomAxis->Minimum = 0;
Grafico->BottomAxis->Maximum = 25;
tempo=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
Grafico->ZoomPercent(120);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
Grafico->ZoomPercent(60);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer5Timer(TObject *Sender)
{
valor[0]=atoi(Label7->Caption.c_str());
Series1->Add(valor[0],"",clRed);
Series2->Add(valor[1],"",clGreen);
Series3->Add(atoi(Edit3->Text.c_str())+atoi(Edit6->Text.c_str()),"",clYellow);
Series4->Add(atoi(Edit3->Text.c_str())-atoi(Edit6->Text.c_str()),"",clBlue);
tempo++;
if (tempo > 70)
{
Grafico->BottomAxis->Minimum ++;
Grafico->BottomAxis->Maximum ++;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1Change(TObject *Sender)
{
if(ComboBox1->Text!="")
{
ComPort1->DeviceName = ComboBox1->Text;
Configurar->Enabled = true;
Abrir_Porta->Enabled = true;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Abrir_PortaClick(TObject *Sender)
{
Timer3->Enabled = true; // liga timer antes de definir opid !
ComPort1->DeviceName = ComboBox1->Text;
ComPort1->Active = true;
StatusBar1->Panels->operator [](0)->Text= "Com Ativa";
StatusBar1->Panels->operator [](1)->Text= "Gr�fico Ligado";
Timer5->Enabled = true;
Button3->Caption = "Pause";
opid[0]='L';
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ConfigurarClick(TObject *Sender)
{
ComPort1->ConfigDialog();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Timer5->Enabled=!Timer5->Enabled;
if(Timer5->Enabled==1)
{
Button3->Caption = "Pause";
StatusBar1->Panels->operator [](1)->Text="Gr�fico Ligado";
}
else
{
Button3->Caption = "Liga";
StatusBar1->Panels->operator [](1)->Text="Gr�fico Pausado";
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox1Click(TObject *Sender)
{
if(CheckBox1->State==cbChecked)
{
if(CheckBox2->State==cbChecked)
CheckBox2->State=cbUnchecked;
modo[0]='A';
modo[1]='R';
TrackBar1->Enabled=false;
Edit1->Enabled=false;
}
else
{
modo[0]='-';
modo[1]='-';
TrackBar1->Enabled=true;
Edit1->Enabled=true;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox2Click(TObject *Sender)
{
if(CheckBox2->State==cbChecked)
{
if(CheckBox1->State==cbChecked)
CheckBox1->State=cbUnchecked;
modo[0]='A';
modo[1]='C';
TrackBar2->Enabled=false;
Edit2->Enabled=false;
}
else
{
modo[0]='-';
modo[1]='-';
TrackBar2->Enabled=true;
Edit2->Enabled=true;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
int v1;
v1=atoi(Edit3->Text.c_str());
Label17->Caption=v1;
Label17->Caption=Label17->Caption+AnsiString("�");
CGauge2->Progress=v1;
ComPort1->WriteChar('A');
ComPort1->WriteChar('L');
ComPort1->WriteChar('0');
if(v1<100)
ComPort1->WriteChar('0');
if(v1<10)
ComPort1->WriteChar('0');
ComPort1->WriteString(v1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
int MV_SUP;
MV_SUP=atoi(Edit4->Text.c_str());
ComPort1->WriteChar('A');
ComPort1->WriteChar('S');
ComPort1->WriteChar('0');
if(MV_SUP<100)
ComPort1->WriteChar('0');
if(MV_SUP<10)
ComPort1->WriteChar('0');
ComPort1->WriteString(MV_SUP);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
int MV_INF;
MV_INF=atoi(Edit5->Text.c_str());
ComPort1->WriteChar('A');
ComPort1->WriteChar('I');
ComPort1->WriteChar('0');
if(MV_INF<100)
ComPort1->WriteChar('0');
if(MV_INF<10)
ComPort1->WriteChar('0');
ComPort1->WriteString(MV_INF);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button7Click(TObject *Sender)
{
int hist;
hist=atoi(Edit6->Text.c_str());
ComPort1->WriteChar('A');
ComPort1->WriteChar('H');
ComPort1->WriteChar('0');
if(hist<100)
ComPort1->WriteChar('0');
if(hist<10)
ComPort1->WriteChar('0');
ComPort1->WriteString(hist);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button9Click(TObject *Sender)
{
int Ti;
Ti=atoi(Edit7->Text.c_str());
ComPort1->WriteChar('P');
ComPort1->WriteChar('I');
ComPort1->WriteChar('0');
if(Ti<100)
ComPort1->WriteChar('0');
if(Ti<10)
ComPort1->WriteChar('0');
ComPort1->WriteString(Ti);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button8Click(TObject *Sender)
{
int K;
K=atoi(Edit7->Text.c_str());
ComPort1->WriteChar('P');
ComPort1->WriteChar('K');
ComPort1->WriteChar('0');
if(K<100)
ComPort1->WriteChar('0');
if(K<10)
ComPort1->WriteChar('0');
ComPort1->WriteString(K);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button10Click(TObject *Sender)
{
int Td;
Td=atoi(Edit7->Text.c_str());
ComPort1->WriteChar('P');
ComPort1->WriteChar('D');
ComPort1->WriteChar('0');
if(Td<100)
ComPort1->WriteChar('0');
if(Td<10)
ComPort1->WriteChar('0');
ComPort1->WriteString(Td);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button11Click(TObject *Sender)
{
ComPort1->WriteString(Edit10->Text.c_str());
modo[0]='P';
modo[1]='R';
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ComPort1->WriteChar('P');
ComPort1->WriteChar('E');
ComPort1->WriteChar('0');
ComPort1->WriteChar('0');
ComPort1->WriteChar('0');
ComPort1->WriteChar('0');
}
//---------------------------------------------------------------------------